﻿using System;
using System.Collections.Generic;
using System.Linq;

using System.Web;
using System.Web.Mvc;

using loveMotorcade.web.Filters;
using loveMotorcade.web.Models;

namespace loveMotorcade.web.Controllers
{
    using Classes;

    public class userController : Controller
    {
        //
        // GET: /Account/Login

        [AllowAnonymous]
        public ActionResult Login(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            return View();
        }

        //
        // POST: /Account/Login

        [HttpPost]
        [ValidateAntiForgeryToken()]
        public ActionResult Login(LoginModel model, string returnUrl)
        {
            string psw=  model.Password.MD5();
            ModelContainer db = new ModelContainer();
            user u = db.user.FirstOrDefault(d => d.name == model.UserName && d.psw == psw);
            if(u==null){
                ViewData.ModelState.AddModelError("","不存在该用户或用户、密码错误");
                return View(model);

            }
            u.lastLoginDate = DateTime.Now;
            db.SaveChanges();

            userC.LoginUser = u;

            ViewData.Model=1;
            return RedirectToAction("index","home");           


        }

        public ActionResult LoginOut()
        {

            userC.LoginOut();
            return RedirectToAction("Index", "Home");
        }




    }
}
